import { _decorator, Component, AudioClip, Node, AudioSource, NodeEventType, Toggle } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AudioMgr')
export default class AudioMgr extends Component {

    @property(AudioSource)
    private audioSource: AudioSource | null = null;

    @property([AudioClip])
    private audioClip: AudioClip[] = [];

    @property(Node)
    private settings: Node | null = null;

    @property({ type:Toggle })
    musicToggle: Toggle = null;

    @property({ type:Toggle })
    soundToggle: Toggle = null;

    @property({ type:Toggle })
    animaToggle: Toggle = null;



    private clip: AudioClip | null = null;

    private isOpenMusicEffects: boolean = true; //是否开启音效，默认为true;
    private isOpenBgm: boolean = true;   //是否开启背景音乐，默认为true;
    private isOpenAnima: boolean = true;   //是否开启动画特效，默认为true;

    private show(nd: Node): void {
        nd.active = true;
    }

    private hide(nd: Node): void {
        nd.active = false;
    }


     onLoad() {
        this.soundToggle.node.on('toggle',this.soundListener,this);
        this.musicToggle.node.on('toggle',this.musicListener,this);
        this.animaToggle.node.on('toggle',this.animaListener,this);
        
    }

    /** 播放背景音乐 */
    public onPlayBgm(clip: AudioClip, volume: number = 1.0): void {
        this.audioSource.clip = clip;
        this.audioSource.play();
        this.audioSource.volume = volume;
        this.audioSource.loop = true;
        this.audioSource.playOnAwake = true;
        // cc.audioEngine.playMusic(clip, loop);
    }

    /** 播放音效 */
    public onPlayOneShot(clip: AudioClip, volume: number = 1.0) {
        if (this.isOpenMusicEffects !== true) {
            return;
        }
        this.audioSource.playOneShot(clip, volume);
    }

    /** 播放背景音乐 */
    public play(): void {
        if (this.isOpenBgm !== true) {
            return;
        }
        this.audioSource.play();
    }
    /** 停止播放 */
    public stop(): void {
        this.audioSource.stop();
    }

    /** 暂停播放 */
    public pause(): void {
        this.audioSource.pause();
    }

    /** 恢复播放 */
    public resume(): void {
        if (this.isOpenBgm !== true) {
            return;
        }
        this.audioSource.play();
    }

    /** 获取音效 */
    public  getAudioName(audioName: string): AudioClip {

        switch (audioName) {
            case "click"://点击音效
                this.clip = this.audioClip[0];
                break;
            case "girl_bonus"://彩金
                    this.clip = this.audioClip[1];
                    break;    
            case "yao_touzi"://摇骰子
                    this.clip = this.audioClip[2];
                    break;
            case "open_cup"://开盖
                    this.clip = this.audioClip[3];
                    break;
            case "close_cup"://关盖
                    this.clip = this.audioClip[4];
                    break;          
            
            default:
                break;
        }
        return this.clip;

    }



    /**
     * 退出声音管理
     */
     onClickExitBtn(): void {
        let audioName = this.getAudioName("click");
        this.onPlayOneShot(audioName,1);
        this.hide (this.settings) ;
    }

    /**
    * 打开声音管理
     */
     onClickShowSettings(): void {
     this. clickEffect();
     this.show (this.settings) ;
     }

     private soundListener() {
        this. clickEffect();
        this.isOpenMusicEffects = this.soundToggle.isChecked;
       }
     

     private musicListener() {
        this. clickEffect();
        this.isOpenBgm = this.musicToggle.isChecked;
        if (this.musicToggle.isChecked) {
            this.resume();
        }else{
            this.pause();
        }
       }
     

   private animaListener() {
    this. clickEffect();
    this.isOpenAnima=this.animaToggle.isChecked;
   }

   public clickEffect(){
    let audioName = this.getAudioName("click");
    this.onPlayOneShot(audioName,1);
   }
   public isOpenAnimaFlag(){
    return this.isOpenAnima;
   }
   public closeAnima(){
    this.animaToggle.isChecked=false;
    
   }
   
}


